package com.wiretap.action.admin.manage;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.util.ResolverUtil.IsA;
import com.wiretap.action.WiretapActionSupport;
import com.wiretap.po.Administrator;
import com.wiretap.service.AdminService;
import com.wiretap.util.CommenUtil;
import com.wiretap.util.Constants;
import com.wiretap.util.EncoderUtil;

public class AdministratorManageAction extends WiretapActionSupport {

	private static final long serialVersionUID = 2562036265533334167L;
	private static final String CONTENT_TYPE = "text/html; charset=utf-8";
	private List<Administrator> admins;
	private AdminService adminService;
	private String aid;
	private String password;
	private String username;

	@Override
	public String execute() throws Exception {
		if (isSuperAdmin()) {
			admins = adminService.getAllAdministrator();
			return SUCCESS;
		} else {
			return "permission";
		}
	}

	public void editAdmin() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (isSuperAdmin()) {
			if (CommenUtil.isEmpty(username) || CommenUtil.isEmpty(password)) {
				out.print("failed");
			} else {
				// 新增用户
				if (CommenUtil.isEmpty(aid)) {
					Administrator administrator = new Administrator();
					administrator.setUsername(username);
					administrator.setPassword(password);
					administrator.setRegistTime(new Date());
					boolean result = adminService.addAdministator(administrator);
					if (result) {
						out.print("success");
					} else {
						out.print("exist");
					}
				}// 修改密码
				else {
					Administrator admin = adminService.getAdministratorbyName(username);
					if (admin != null) {
						admin.setPassword(EncoderUtil.encode(password, Constants.ENCODE_MD5));
						adminService.changePassword(admin);
						out.print("success");
					} else {
						out.print("failed");
					}
				}
			}
		} else {
			out.print("failed");
		}
		out.flush();
		out.close();
	}

	public void deleteAdmin() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (isSuperAdmin()) {
			if(CommenUtil.isEmpty(aid)){
				out.print("failed");
			}else{
				adminService.deleteAdmin(aid);
				out.print("success");
			}
		}else{
			out.print("failed");
		}
		out.flush();
		out.close();
	}

	public List<Administrator> getAdmins() {
		return admins;
	}

	public void setAdmins(List<Administrator> admins) {
		this.admins = admins;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public String getAid() {
		return aid;
	}

	public void setAid(String aid) {
		this.aid = aid;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}
